package com.hakim.wfb.admin.filter;

import com.hakim.wfb.admin.enums.ErrorCodeEnum;
import com.hakim.wfb.dao.AdminDao;
import com.hkfs.fundamental.api.data.ResultEx;
import com.hkfs.fundamental.validate.ValidateFilter;
import com.hkfs.fundamental.validate.annotaion.ValidateFilterBean;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.beans.factory.annotation.Autowired;

import javax.validation.constraints.NotNull;
import java.util.HashMap;
import java.util.Map;

/**
 *
 */
@ValidateFilterBean
public class ModifyAdminFilter extends ValidateFilter<ModifyAdminFilter.ModifyUserFilterParam> {

    @Autowired
    private AdminDao userDao;

    @Override
    protected void prcoess(ModifyUserFilterParam param, ResultEx resultEx) {
        Map<String, Object> existsParam = new HashMap<String, Object>();
        existsParam.put("id", param.getId());
        existsParam.put("userName", param.getUserName());
        int count = userDao.modifyExists(existsParam);
        if (count > 0) {
            resultEx.makeResult(ErrorCodeEnum.USER_NAME_EXISTS.getCode(), ErrorCodeEnum.USER_NAME_EXISTS.getMessage());
            return;
        }
        resultEx.makeSuccessResult();
    }

    public static class ModifyUserFilterParam {
        /**
         * 用户ID
         */
        @NotNull(message = "用户ID不能为空")
        private int id;
        /**
         * 登录用户名
         */
        @NotEmpty(message = "用户名不能为空")
        private String userName;

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public String getUserName() {
            return userName;
        }

        public void setUserName(String userName) {
            this.userName = userName;
        }
    }
}
